<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of indexAction
 *
 * @author wuxiaofeng
 */
class IndexAction extends MenuAction{
    //put your code here
    function index(){

         $Content=M('Content');
         $Category=M('Category');
         $ContentThumb = M('ContentThumb');
         import("@.ORG.Page");
        $channel = isset($_GET['channel'])?intval($_GET['channel']):0;
      
        if($channel){
            $oChannel = M('Channel')->where('channel_id='.$channel)->find();
            $this->assign('channel', $oChannel);
            $count=$Content->query("SELECT COUNT(*) as cc FROM `c_content` as content LEFT JOIN `c_content_thumb` on `content`.content_id=`c_content_thumb`.thumb_content WHERE  content.channel_id=$channel  AND content.content_state=1 AND content.content_isthumb=1");
        }  else {
            /*$contentpic1=$Content->query("SELECT * FROM `c_content` LEFT JOIN `c_content_thumb` on `c_content`.content_id=`c_content_thumb`.thumb_content WHERE content_id IN(
SELECT MAX(content_id) FROM `c_content` GROUP BY channel_id) AND content_state=1 AND content_isthumb=1 ORDER BY content_id DESC" );
            $contentpic2=$Content->query("SELECT * FROM `c_content` LEFT JOIN `c_content_thumb` on `c_content`.content_id=`c_content_thumb`.thumb_content WHERE content_id NOT IN(
SELECT MAX(content_id) FROM `c_content` GROUP BY channel_id) AND content_state=1 AND content_isthumb=1 ORDER BY content_id DESC LIMIT 2");*/
            $count=$Content->query("SELECT COUNT(*) as cc FROM `c_content` LEFT JOIN `c_content_thumb` on `c_content`.content_id=`c_content_thumb`.thumb_content 
                                               WHERE content_state=1 AND content_isthumb=1");
        }
         $Page=new Page($count[0]['cc'],4);
         $Page->setConfig('theme',' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %first%  %prePage%  %linkPage%  %nextPage%  %downPage% %end%');
         $show=$Page->show();
         
        
        if($channel){
            $oChannel = M('Channel')->where('channel_id='.$channel)->find();
            $this->assign('channel', $oChannel);
            $contentpic1=$Content->query("SELECT * FROM `c_content` as content LEFT JOIN `c_content_thumb` on `content`.content_id=`c_content_thumb`.thumb_content WHERE  content.channel_id=$channel  AND content.content_state=1 AND content.content_isthumb=1 ORDER BY content.content_id DESC LIMIT $Page->firstRow,$Page->listRows");
        }  else {
            /*$contentpic1=$Content->query("SELECT * FROM `c_content` LEFT JOIN `c_content_thumb` on `c_content`.content_id=`c_content_thumb`.thumb_content WHERE content_id IN(
SELECT MAX(content_id) FROM `c_content` GROUP BY channel_id) AND content_state=1 AND content_isthumb=1 ORDER BY content_id DESC" );
            $contentpic2=$Content->query("SELECT * FROM `c_content` LEFT JOIN `c_content_thumb` on `c_content`.content_id=`c_content_thumb`.thumb_content WHERE content_id NOT IN(
SELECT MAX(content_id) FROM `c_content` GROUP BY channel_id) AND content_state=1 AND content_isthumb=1 ORDER BY content_id DESC LIMIT 2");*/
            $contentpic1=$Content->query("SELECT *FROM `c_content` LEFT JOIN `c_content_thumb` on `c_content`.content_id=`c_content_thumb`.thumb_content 
                                               WHERE content_state=1 AND content_isthumb=1 ORDER BY content_id DESC LIMIT $Page->firstRow,$Page->listRows");
        }
        
         
         /*$contentwithpic=$Content->join('LEFT JOIN c_content_thumb ON c_content.content_id=c_content_thumb.thumb_content')->order('content_id desc')->select();
         //$thumbfromcont=$ContentThumb->join('LEFT JOIN c_content ON c_content_thumb.thumb_content=c_content.content_id')->select();
         var_dump($contentwithpic);
        // var_dump($thumbfromcont);
         exit;*/
         
        foreach($contentpic1 as $value)
         {
            $contentcate0=$Category->where('category_id='.$value['category_id'].' AND category_state=1')->find();
            $contentcateone[]=$contentcate0['category_name'];
            $contentcateone[]=$value;
            $contentcateone[]=date('Y-m-d',strtotime($value['content_time']));
            $contentcates[]=$contentcateone;
            unset($contentcateone);
         }
         
         
        
         $this->assign('contentcates',$contentcates);

        $channel = isset($_GET['channel'])?intval($_GET['channel']):0;
        if($channel){
            $oChannel = M('Channel')->where('channel_id='.$channel)->find();
            $this->assign('channel', $oChannel);
        }
        $this->assign('page',$show);
        $this->display();
        
    }
    
    
    function latest() {
        $Content = M('Content');
        $channel = isset($_GET['channel'])?intval($_GET['channel']):0;
        if($channel){
            $Contentself=$Content->field('content_id,content_title,content_description,content_time,cct.thumb_name')
                    ->join('c_content_thumb AS cct ON cct.thumb_content=c_content.content_id')
                    ->where('content_title!="" AND channel_id='.$channel)
                    ->order('content_id desc')
                    ->limit(10)
                    ->select(); 
        }else{
            $Contentself=$Content->field('content_id,content_title,content_description,content_time,cct.thumb_name')
                    ->join('c_content_thumb AS cct ON cct.thumb_content=c_content.content_id')
                    ->where('content_title!=""')
                    ->order('content_id desc')
                    ->limit(10)
                    ->select(); 
        }
        $data = array();
        foreach ($Contentself as $key => $content) {
            $datetime = strtotime($content['content_time']);
            $now = time();
            $difftime = $now-$datetime;
            if($difftime<300){
                $time='';
                $timemsg = '刚刚发布';
            }elseif($difftime<3600){
                $time = intval($difftime/60);
                $timemsg = '分钟前发布';
            }elseif($difftime<24*3600){
                $time = intval($difftime/3600);
                $timemsg = '小时前发布';
            }else{
                $time = intval($difftime/(24*3600));
                $timemsg = '天前发布';
            }
            $data[] = array(
                'id'=>$content['content_id'],
                'title'=>$content['content_title'],
                'description'=>$content['content_description'],
                'time'=>$time,
                'timemsg'=>$timemsg,
                'img'=>$content['thumb_name']
            );
        }
        $this->ajaxReturn($data);
    }
    function popular() {
        $Content = M('Content');
        
        $channel = isset($_GET['channel'])?intval($_GET['channel']):0;
        if($channel){
            $Contentself=$Content->field('content_id,content_title,content_description,content_time,cct.thumb_name')
                    ->join('c_content_thumb AS cct ON cct.thumb_content=c_content.content_id')->where('content_title!="" AND channel_id='.$channel)->order('content_click_count desc')->limit(10)->select();
        }else{
            $Contentself=$Content->field('content_id,content_title,content_description,content_time,cct.thumb_name')
                    ->join('c_content_thumb AS cct ON cct.thumb_content=c_content.content_id')->where('content_title!=""')->order('content_click_count desc')->limit(10)->select(); 
        }
        $data = array();
        foreach ($Contentself as $key => $content) {
            $datetime = strtotime($content['content_time']);
            $now = time();
            $difftime = $now-$datetime;
            if($difftime<300){
                $time='';
                $timemsg = '刚刚发布';
            }elseif($difftime<3600){
                $time = intval($difftime/60);
                $timemsg = '分钟前发布';
            }elseif($difftime<24*3600){
                $time = intval($difftime/3600);
                $timemsg = '小时前发布';
            }else{
                $time = intval($difftime/(24*3600));
                $timemsg = '天前发布';
            }
            $data[] = array(
                'id'=>$content['content_id'],
                'title'=>$content['content_title'],
                'description'=>$content['content_description'],
                'time'=>$time,
                'timemsg'=>$timemsg,
                'img'=>$content['thumb_name']
            );
        }
        $this->ajaxReturn($data);
    }
}

?>
